<?php
/**
 * Created by PhpStorm.
 * User: 小蛮哼哼哼
 * Email: 243194993@qq.com
 * Date: 2022/3/22
 * Time: 14:19
 * motto: 现在的努力是为了小时候吹过的牛逼！
 */

namespace app\api\controller\v1;

use app\admin\controller\buddha\ArticleClick;
use app\common\model\BuddhaArticle;
use app\common\model\BuddhaArticleClick;
use app\common\lib\Show;
use app\common\lib\Status;

class Article extends Base
{
    public function getArticleByDetail()
    {
        $id = $this->request->param('id', 0, 'intval');
        $openid = $this->request->param('openid', '', 'trim');
        if ($id == 0) {
            return Show::json_show(Status::mini_error, "error");
        }
        //根据id查询文章
        $obj = new BuddhaArticle();
        $obj->startTrans();
        try {
            $insert_data = [
                "article_id" => $id,
                "openid" => $openid
            ];
            //新增查看记录
            (new BuddhaArticleClick())->insertSaveArticle($insert_data);
            $obj->where("id",$id)->inc("clickNum")->update();
            $article = $obj->getBaseById($id, 'id,title,short_title,description,create_time,clickNum');
            $obj->commit();
        } catch (\Exception $e) {
            $obj->rollback();
            return Show::json_show(Status::mini_error, $e->getMessage());
        }
        if (!$article) {
            return Show::json_show(Status::mini_error, "详情不存在");
        }
        $row = $article->toArray();
        $lists = [];
        if(!empty($row)){
            $lists = $row;
            $lists['description'] = htmlspecialchars_decode($row['description']);
        }
        return Show::json_show(Status::mini_normal, "ok", $lists);
    }
    public function getArticleByList(){
        $page = $this->request->param('page',1,'intval');
        $limit = 10;
        $lists = (new BuddhaArticle())->getPaginateByNormalListInfo([],$limit);
        return Show::json_show(Status::mini_normal,"ok",$lists);

    }
}